package starfield.util;

import org.json.simple.*;
import org.json.simple.parser.ParseException;
import starfield.ParsingException;

public class JSONUtils {
	
	public static String removeComments(String s) {
		StringBuilder sb = new StringBuilder();
		
		for (String line : s.split("\n")) {
			if (!line.trim().startsWith("//")) {
				sb.append(line).append("\n");
			}
		}
		
		return sb.toString();
	}
	
	public static JSONObject parseObject(String s) throws ParsingException {
		try {
			Object o = JSONValue.parseWithException(s);
			
			if (o instanceof JSONObject) {
				return (JSONObject)o;
			} else {
				throw new ParsingException("Root of parsed JSON is not a valid object.");
			}
		} catch (ParseException ex) {
			throw new ParsingException("Failed to parse JSON: " + ex.getMessage(), ex);
		}
	}
	
	public static String getString(JSONObject o, Object key) throws ParsingException {
		Object v = o.get(key);

		if (v instanceof String) {
			return (String)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static int getInt(JSONObject o, Object key) throws ParsingException {
		Object v = o.get(key);

		if (v instanceof Integer) {
			return (int)v;
		} else if (v instanceof Long) {
			return (int)(long)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static double getDouble(JSONObject o, Object key) throws ParsingException {
		Object v = o.get(key);

		if (v instanceof Double) {
			return (double)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static boolean getBoolean(JSONObject o, Object key) throws ParsingException {
		Object v = o.get(key);

		if (v instanceof Boolean) {
			return (boolean)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static JSONArray getArray(JSONObject o, Object key) throws ParsingException {
		Object v = o.get(key);

		if (v instanceof JSONArray) {
			return (JSONArray)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static JSONObject getObject(JSONObject o, Object key) throws ParsingException {
		Object v = o.get(key);

		if (v instanceof JSONObject) {
			return (JSONObject)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static String getString(JSONArray a, int i) throws ParsingException {
		Object v = a.get(i);

		if (v instanceof String) {
			return (String)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static int getInt(JSONArray a, int i) throws ParsingException {
		Object v = a.get(i);

		if (v instanceof Integer) {
			return (int)v;
		} else if (v instanceof Long) {
			return (int)(long)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static double getDouble(JSONArray a, int i) throws ParsingException {
		Object v = a.get(i);

		if (v instanceof Double) {
			return (double)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static boolean getBoolean(JSONArray a, int i) throws ParsingException {
		Object v = a.get(i);

		if (v instanceof Boolean) {
			return (boolean)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static JSONArray getArray(JSONArray a, int i) throws ParsingException {
		Object v = a.get(i);

		if (v instanceof JSONArray) {
			return (JSONArray)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
	
	public static JSONObject getObject(JSONArray a, int i) throws ParsingException {
		Object v = a.get(i);

		if (v instanceof JSONObject) {
			return (JSONObject)v;
		} else {
			throw new ParsingException(ParsingException.MSG_TYPE_MISMATCH);
		}
	}
}
